package com.linkin.homepage.base.util;


import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;


public class ViewUtils {

    public static int[] getViewLocationOnScreen(View view) {
        int[] location = new int[2];
        try {
            view.getLocationOnScreen(location);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return location;
    }

    public static boolean setViewWidth(View view, int width) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams != null && layoutParams.width != width) {
            layoutParams.width = width;
            return true;
        }
        return false;
    }

    public static boolean setViewHeight(View view, int height) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams != null && layoutParams.height != height) {
            layoutParams.height = height;
            return true;
        }
        return false;
    }

    public static boolean setViewWeight(View view, float weight) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof LinearLayout.LayoutParams) {
            if (((LinearLayout.LayoutParams) layoutParams).weight != weight) {
                ((LinearLayout.LayoutParams) layoutParams).weight = weight;
                return true;
            }
        }
        return false;
    }

    public static boolean setViewMarginLeft(View view, int leftMargin) {
        return setViewMarginLeft(view, leftMargin, false);
    }

    public static boolean setViewMarginLeft(View view, int leftMargin, boolean requestLayout) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)layoutParams;
            if (params.leftMargin != leftMargin) {
                params.leftMargin = leftMargin;
                if (requestLayout) {
                    view.requestLayout();
                }
                return true;
            }
        }
        return false;
    }

    public static boolean setViewMarginRight(View view, int leftMargin) {
        return setViewMarginRight(view, leftMargin, false);
    }

    public static boolean setViewMarginRight(View view, int rightMargin, boolean requestLayout) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)layoutParams;
            if (params.rightMargin != rightMargin) {
                params.rightMargin = rightMargin;
                if (requestLayout) {
                    view.requestLayout();
                }
                return true;
            }
        }
        return false;
    }

    public static boolean setViewMarginTop(View view, int topMargin) {
        return setViewMarginTop(view, topMargin, false);
    }

    public static boolean setViewMarginTop(View view, int topMargin, boolean requestLayout) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)layoutParams;
            if (params.topMargin != topMargin) {
                params.topMargin = topMargin;
                if (requestLayout) {
                    view.requestLayout();
                }
                return true;
            }
        }
        return false;
    }

    public static boolean setViewMarginBottom(View view, int bottomMargin, boolean requestLayout) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)layoutParams;
            if (params.bottomMargin != bottomMargin) {
                params.bottomMargin = bottomMargin;
                if (requestLayout) {
                    view.requestLayout();
                }
                return true;
            }
        }
        return false;
    }

    public static int getViewMarginLeft(View view) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            return ((ViewGroup.MarginLayoutParams)layoutParams).leftMargin;
        }
        return 0;
    }

    public static int getViewMarginRight(View view) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            return ((ViewGroup.MarginLayoutParams)layoutParams).rightMargin;
        }
        return 0;
    }

    public static int getViewMarginTop(View view) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            return ((ViewGroup.MarginLayoutParams)layoutParams).topMargin;
        }
        return 0;
    }

    public static int getViewMarginBottom(View view) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            return ((ViewGroup.MarginLayoutParams)layoutParams).bottomMargin;
        }
        return 0;
    }

    public static boolean setViewPaddingLeft(View view, int paddingLeft) {
        if (view.getPaddingLeft() != paddingLeft) {
            int top = view.getPaddingTop();
            int right = view.getPaddingRight();
            int bottom = view.getPaddingBottom();
            view.setPadding(paddingLeft, top, right, bottom);
            return true;
        }
        return false;
    }

    public static boolean setViewPaddingRight(View view, int paddingRight) {
        if (view.getPaddingRight() != paddingRight) {
            int left = view.getPaddingLeft();
            int top = view.getPaddingTop();
            int bottom = view.getPaddingBottom();
            view.setPadding(left, top, paddingRight, bottom);
            return true;
        }
        return false;
    }

    public static boolean setViewPaddingTop(View view, int paddingTop) {
        if (view.getPaddingTop() != paddingTop) {
            int left = view.getPaddingLeft();
            int right = view.getPaddingRight();
            int bottom = view.getPaddingBottom();
            view.setPadding(left, paddingTop, right, bottom);
            return true;
        }
        return false;
    }

    public static boolean setViewPaddingBottom(View view, int paddingBottom) {
        if (view.getPaddingBottom() != paddingBottom) {
            int left = view.getPaddingLeft();
            int top = view.getPaddingTop();
            int right = view.getPaddingRight();
            view.setPadding(left, top, right, paddingBottom);
            return true;
        }
        return false;
    }
}
